<?php
/**
	Author: Ziming Zhai
	Date: 04/14/2011
*/

require_once("config.inc");

class connection_pool{
	private $mysql;
	private static $instance;
	
	private function __construct(){
		$this->mysql = mysql_connect(MYSQL_HOST.":".MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD);
		if (!$this->mysql) {
    		die('Could not connect: ' . mysql_error());
		}
		mysql_select_db(MYSQL_DATABASE,$this->mysql);
	}
	
    // The singleton method
    public static function connection(){
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }
	
	
	// general query
	public function query($sql){
		//echo $sql."<br>";
		$result = mysql_query($sql, $this->mysql);
		if(!$result){
			echo mysql_error();
			return 0;
		}
		else{
			$result_array = array();
			while ($row = mysql_fetch_assoc($result)) {
    			  array_push($result_array,$row);
			}
			return $result_array;
		}
	}
	
	
	// select data with constraint
	public function select($table_name, $extract= array(),$constraint= array()){
		$query = "select ";
		if(count($extract)==0)
			$query .= " * ";
		else{
			$query .= implode(",",$extract);
		}
		$query .= " from ".$table_name." ";
		if(count($constraint)>0){
			$where = "where ";
			$clause = array();
			foreach($constraint as $key=>$value){
				array_push($clause, $table_name.".".$key." = '".$value."'");
			}
			$where .= implode(" AND ", $clause);
			$query .= $where;
		}
		$result = $this->query($query);	
		return $result;
	}
	
	
	// create table
	public function create_table($table_name, $columns = array()){
		if(count($columns)==0) return false;
		$query = "create table if not exists ".$table_name." (";
		foreach($columns as $line){
			list($c_name,$c_type,$c_length) = $line;
			$tuple = "";
			switch(strtolower($c_type)){
				case "int": $tuple .= $c_name." INT,";
						break;
				case "varchar": $tuple .= $c_name." varchar(".$c_length."),";
						break;
				default:  
					$tuple .= $c_name." varchar(".$c_length."),";
					break;
			}
			$query .= $tuple;
		}
		$query = substr($query, 0, -1).")";
		if(mysql_query($query)) return true;
		else return false;

	}
	
	
	// Insert data into centain table
	public function insert_data($table_name, $values= array()){
		if(count($values)==0) return false;
		$query = "insert into ".$table_name;
		$key_array = array();
		$value_array = array();
		foreach($values as $key=>$value){
			array_push($key_array,$key);
			array_push($value_array,"'".$value."'");
		}
		
		$query .= " (".implode(",",$key_array).")";
		$query .= " values (".implode(",",$value_array).")";
		if(mysql_query($query)) return true;
		else return false;

	}
	
	public function update($table_name, $values=array(), $constraints=array()){
		$query = "update ".$table_name." set ";
		$arr = array();
		foreach($values as $key=>$value){
			array_push($arr, $key."='".$value."'");
		}
		$query .= implode(" , ", $arr);
		$arr = array();
		foreach($constraints as $key=>$value){
			array_push($arr, $key."='".$value."'");
		}
		$query .= " where ".implode(" AND ", $arr);
		mysql_query($query,$this->mysql);
	}
	
	public function if_table_exist($table_name){
		$result = $this->query("show tables like '".$table_name."'");
		if($result && count($result)==1) return true;
		else return false;
	}
	
	public function if_record_exist($table_name, $values=array()){
		$query = "select * from ".$table_name;
		if(count($values)>0){
			$query .=" where ";
			$query .= implode(" AND ", $values);
		}
		//echo $query."<br>";
		$result = $this->query($query);
		if(count($result)>0) return true;
		else return false;
	}
	
	
	// Prevent users to clone the instance
    public function __clone()
    {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
	
	
	function __destruct(){
		mysql_close($this->mysql);	
	}

}


?>